﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameContorller : MonoBehaviour {
    private int gameLevel = 0;
    public static int playerNum = 4;

    public int wallCount;

	public int enemyCount;

	public int xitm;

	public int yitm;

	public int goThroughTime;

    public GameObject[] playerObjs;

    public void ExitGame(){
		Application.Quit ();
	}
	void Start(){
		initGame ();
		this.GetComponent<UIController> ().StartViewWithIndex (0);
	}

	// Update is called once per frame
	void Update () {
		
	}
	//开始下一关
	public void StartGame(){
		StartCoroutine(loadLevel ());
	}

	//关卡控制器
	void levelContorller(int levelCount){
		xitm = 6 + 2 *(levelCount/3);
		yitm = 3 + 2 *(levelCount / 3);
		if (xitm > 16) {
			xitm = 16;
		}
		if (yitm > 9) {
			yitm = 9;
		}
        xitm = 6;
        yitm = 3;

        FollowPlayer.xitm = xitm;
		FollowPlayer.yitm = yitm;

		//x表示地图所有空位的百分比 0-1之间 如：0.2 表示 取整个地图20%的空格子
		float x = (0.3f + (levelCount * 0.07f));
		if (x > 0.7f) {
			x = 0.7f;
		}

		wallCount = (int)(xitm * yitm * x) + 5;

		enemyCount = (int)(levelCount*1.25f + 1);

		//调用地图控制器的初始化地图方法
		this.GetComponent<GenerateLevel>().initMap(wallCount, enemyCount, xitm, yitm);
		this.GetComponent<MapController>().initMap(wallCount, enemyCount, xitm, yitm);
		
		//初始化猪脚
		initPlayer();

		//关闭计时器
		StopCoroutine ("gameTimeCount");

        //开启计时器
        StartCoroutine ("gameTimeCount", (int)(Const.gameMinite * 60)); 

        //调出游戏UI
        this.GetComponent<UIController> ().StartViewWithIndex (1);
	}

	//加载关卡
	IEnumerator loadLevel(){
		gameLevel++;
		UIController.gameLevel = gameLevel;
		this.GetComponent<UIController> ().StartViewWithIndex (2);
        this.GetComponent<UIController>().levelText.text = "3";
        yield return new WaitForSeconds (1.0f);
        this.GetComponent<UIController>().levelText.text = "2";
        yield return new WaitForSeconds(1.0f);
        this.GetComponent<UIController>().levelText.text = "1";
        yield return new WaitForSeconds(1.0f);
        levelContorller (gameLevel);
	}

    public void initPlayer() {
		//生成主角
		foreach (var player in playerObjs)
		{
			player.SetActive(true);
			player.GetComponent<PlayerController>().initPlayer(Const.playerSpeed);
		}
	}
	//游戏结束
	public void gameOver(){
		foreach (var player in playerObjs)
		{
			player.SetActive(false);
		}
		this.GetComponent<UIController> ().StartViewWithIndex (3);
	}

	//重新开始游戏
	public void gameRese(){
		initGame ();
		StartGame ();
	}
	//初始化属性
	public void initGame(){
		this.GetComponent<UIController> ().initScoreValue();
		gameLevel = 0;
		goThroughTime = 0;
		foreach (var player in playerObjs)
		{
			player.SetActive(false);
		}
	}

	IEnumerator gameTimeCount(int count){
		//为游戏时间赋值
		UIController.gameTime = count;
		//倒计时游戏时间
		while (true) {
			yield return new WaitForSeconds (1);
			UIController.gameTime--;
			goThroughTime ++;
			this.GetComponent<MapController> ().produceCoinsArray(goThroughTime);
			if (UIController.gameTime <= 0) {
				// 判断是否产生了第一名 否则加时
				if(!this.GetComponent<UIController> ().isGameFinish())
				{
					UIController.gameTime += 10;
				}
				else
				{
					//游戏结束
					gameOver ();
					break;
				}
				
			}
		}
	}
}